package com.lx.controller;

import com.lx.annotation.CheckPermission;
import com.lx.entity.Category;
import com.lx.service.CategoryService;
import com.lx.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "商品分类管理接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有分类
     */
    @PostMapping("/category/all")
    @ApiOperation(value = "获取所有分类", notes = "获取所有分类列表")
    public Result getAllCategories() {
        List<Category> categories = categoryService.getAllCategories();
        return Result.success(5200, "获取成功").add("categories", categories);
    }
    @PostMapping("/category/pageinfo")
    @ApiOperation(value = "分页查询分类列表", notes = "根据条件查询分页列表")
    public Result getPageInfo(@RequestBody Map<String, Object> params) {
        // 清理空参数
        if (params.containsKey("name") &&
                (params.get("name") == null || params.get("name").toString().isEmpty())) {
            params.remove("name");
        }

        if (params.containsKey("type") &&
                (params.get("type") == null || params.get("type").toString().isEmpty())) {
            params.remove("type");
        }

        return Result.success(5200, "获取分类列表成功").add("pageinfo", categoryService.getPageInfo(params));
    }



    /**
     * 获取一级分类
     */
    @PostMapping("/category/first")
    @ApiOperation(value = "获取一级分类", notes = "获取所有一级分类")
    public Result getFirstLevelCategories() {
        List<Category> categories = categoryService.getFirstLevelCategories();
        return Result.success(5200, "获取成功").add("categories", categories);
    }

    /**
     * 获取二级分类
     */
    @PostMapping("/category/second")
    @ApiOperation(value = "获取二级分类", notes = "获取id下的所有二级分类，不传id默认获取所有二级分类")
    public Result getSecondLevelCategories(@RequestBody(required = false) Map<String, Object> params) {
        List<Category> categories = categoryService.getSecondLevelCategories(params.containsKey("id") ? Integer.parseInt(params.get("id").toString()) : null);
        return Result.success(5200, "获取成功").add("categories", categories);
    }

    /**
     * 获取三级分类
     */
    @PostMapping("/category/third")
    @ApiOperation(value = "获取三级分类", notes = "获取id下的所有三级分类，不传id默认获取所有三级分类")
    public Result getThirdLevelCategories(@RequestBody(required = false) Map<String, Object> params) {
        List<Category> categories = categoryService.getThirdLevelCategories(params.containsKey("id") ? Integer.parseInt(params.get("id").toString()) : null);
        return Result.success(5200, "获取成功").add("categories", categories);
    }

    /**
     * 添加分类
     */
    @CheckPermission(value = "1")
    @PostMapping("/category/add")
    @ApiOperation(value = "添加分类", notes = "添加新分类")
    public Result addCategory(@RequestBody Category category) {
        boolean result = categoryService.addCategory(category);
        if (result) {
            return Result.success(5200, "添加成功");
        }
        return Result.error(5500, "分类名称在同级别中已存在");
    }

    /**
     * 修改分类
     */
    @CheckPermission(value = "1")
    @PostMapping("/category/update")
    @ApiOperation(value = "修改分类", notes = "修改分类信息")
    public Result updateCategory(@RequestBody Category category) {
        //查看是否存在相同分类, 要求：在前端判断相同name不修改。
        int count = categoryService.selectSameName(category.getType(), category.getName());
        if(count <= 0) {
            boolean result = categoryService.updateCategory(category);
            if (result) {
                return Result.success(5200, "修改成功");
            }
            return Result.error(5500, "修改错误！");
        }
        return Result.error(5500, "分类名称在同级别中已存在");

    }

    /**
     * 删除分类
     */
    @CheckPermission(value = "1")
    @PostMapping("/category/delete")
    @ApiOperation(value = "删除分类", notes = "逻辑删除分类")
    public Result deleteCategory(@RequestBody Map<String, Object> params) {
        Integer id = Integer.parseInt(params.get("id").toString());
        boolean result = categoryService.deleteCategory(id);
        if (result) {
            return Result.success(5200, "删除成功");
        }
        return Result.error(5500, "该分类有下级分类或已被商品引用，无法删除");
    }

    /**
     * 获取分类详情
     */
    @PostMapping("/category/detail")
    @ApiOperation(value = "获取分类详情", notes = "根据ID获取分类详情")
    public Result getCategoryById(@RequestBody Integer id) {
        Category category = categoryService.getCategoryById(id);
        if (category != null) {
            return Result.success(5200, "获取成功").add("category", category);
        }
        return Result.error(5500, "未找到分类");
    }
}
